﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Web.UI.WebControls;

namespace OT.Common
{
    #region EnumUtility

    public class EnumUtility
    {
        #region Equal

        static public bool Equal<T>(string str, T enumT)
        {
            if (str == enumT.ToString() || str == Convert.ToInt32(enumT).ToString())
                return true;

            return false;
        }

        #endregion

        #region ToString

        static public string ToString<T>(T enumT)
        {
            return Convert.ToInt32(enumT).ToString();
        }

        #endregion

        #region GetValue

        static public int GetValue<T>(string str)
        {
            string calc = str;

            Type type = typeof(T);
            FieldInfo[] fields = type.GetFields();

            foreach (FieldInfo fInfo in fields)
            {
                if (fInfo.Name.Equals(str))
                {
                    calc = ToString<T>((T)fInfo.GetValue(null));
                    break;
                }
            }

            return int.Parse(calc);
        }

        #endregion

        #region GetEnumValue

        static public T GetEnumValue<T>(string str)
        {
            string calc = str;

            Type type = typeof(T);
            FieldInfo[] fields = type.GetFields();

            T ret = default(T);

            foreach (FieldInfo fInfo in fields)
            {
                if (fInfo.Name.Equals("value__"))
                    continue;

                T t = (T)fInfo.GetValue(null);

                if (Equal<T>(str, t))
                {
                    ret = t;
                    break;
                }
            }

            return ret;
        }

        #endregion

        #region GetEnumItems

        static public object GetEnumItems<T>()
        {
            Type type = typeof(T);
            Array datas = System.Enum.GetValues(type);

            List<EnumItem> items = new List<EnumItem>();

            foreach (T data in datas)
            {
                EnumItem item = new EnumItem();
                item.ID = Convert.ToInt32(data).ToString();
                item.Name = GetEnumCaption(type, data);

                items.Add(item);
            }

            return items;
        }

        static public object GetEnumItems(Type type)
        {
            Array datas = System.Enum.GetValues(type);

            List<EnumItem> items = new List<EnumItem>();

            for ( int i = 0 ; i < datas.Length ; i++ )
            {
                EnumItem item = new EnumItem();
                item.ID = Convert.ToInt32(datas.GetValue(i)).ToString();
                item.Name = GetEnumCaption(type, datas.GetValue(i));

                items.Add(item);
            }

            return items;
        }

        #endregion

        #region GetEnumCaption

        static public string GetEnumCaption(Type aType, object enumData)
        {
            string ret = "";
            FieldInfo[] props = aType.GetFields();

            foreach (FieldInfo fInfo in props)
            {
                if (!fInfo.Name.Equals(enumData.ToString()))
                    continue;

                object[] attrs = fInfo.GetCustomAttributes(false);
                // 根据定义的DatabaseColumn从字典中获取值
                foreach (object attr in attrs)
                {
                    if (attr is CaptionAttribute)
                    {
                        ret = (attr as CaptionAttribute).Caption;
                        break;
                    }
                }

            }

            return ret;
        }

        #endregion

        #region ComboBindEnum

        static public void ComboBindEnum<T>(DropDownList combo)
        {
            combo.DataSource = GetEnumItems<T>();
            combo.DataBind();
            combo.SelectedIndex = 0;
        }

        #endregion


    }

    #endregion

    #region EnumDataSource

    public class EnumDataSource
    {
        Type _enumType;
        public EnumDataSource(Type aEnumType)
        {
            _enumType = aEnumType;
        }
        public List<EnumItem> Select()
        {
            return EnumUtility.GetEnumItems(_enumType) as List<EnumItem>;
        }

        public static IList<EnumItem> Select<T>()
        {
            return EnumUtility.GetEnumItems(typeof(T)) as IList<EnumItem>;
        }
    }

    #endregion

    #region EnumItem

    public class EnumItem
    {
        string _ID = "";
        public string ID
        {
            get { return _ID; }
            set { _ID = value; }
        }

        string _Name = "";
        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

    }

    #endregion

    #region CaptionAttribute

    public class CaptionAttribute : Attribute
    {
        public CaptionAttribute(string caption)
        {
            Caption = caption;
        }

        // 摘要:
        //     映射的列名称
        string _Caption = "";
        public string Caption
        {
            get { return _Caption; }
            set { _Caption = value; }
        }
    }

    #endregion

}
